<template>
  <div class="news">
    <van-nav-bar
      title="新闻动态"
      left-text="返回"
      left-arrow
      @click-left="goBack"
    />
    <van-tabs
      v-model="active"
      type="card"
      @click="changeNews"
    >

      <van-tab title="公司动态">
        <van-pull-refresh
          v-model="refreshing"
          @refresh="onRefresh"
        >
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell
              v-for="item in list"
              :key="item.id"
              :title="item.time+item.news"
            />
          </van-list>
        </van-pull-refresh>
      </van-tab>

      <van-tab title="行业新闻">
        <van-pull-refresh
          v-model="refreshing"
          @refresh="onRefresh"
        >
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell
              v-for="item in list"
              :key="item.id"
              :title="item.time+item.news"
            />
          </van-list>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>

  </div>
</template>

<script>
import { getNewsData } from '@/api/news'
export default {
  name: 'News',
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      active: 2,
      newsInfo: {}
    }
  },
  methods: {
    onLoad() {
      setTimeout(() => {
        for (let i = 0; i < 10; i++) {
          this.list.push(this.newsInfo[i])
        }
        this.loading = false

        if (this.list.length >= this.newsInfo.length) {
          this.finished = true
        }
      }, 1000)
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
      location.reload()
    },
    goBack() {
      this.$router.back()
    },
    changeNews() {
      this.newsInfo = {}
      this.list = []
      getNewsData()
        .then(res => {
          this.newsInfo = res.data.data.newsInfo
          this.onLoad()
          // 清空列表数据
          this.finished = false

          // 重新加载数据
          // 将 loading 设置为 true，表示处于加载状态
          this.loading = true
          this.onLoad()
        })
    }
  },
  created() {
    getNewsData()
      .then(res => {
        this.newsInfo = res.data.data.newsInfo
      })
  }
}
</script>

<style lang="less" scoped>
.news {
  margin: 0;
}
/deep/.van-tabs__nav--card {
  margin: 0;
}
</style>
